using B.S.XZYData.Api.Read.Application.Command.Route;
using B.S.XZYData.Api.Read.Dto.Route;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 工艺路线读操作控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RouteController : ControllerBase
    {
        private readonly IMediator _mediator;

        public RouteController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取工艺路线分页列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="routeCode">工艺路线编号（模糊查询）</param>
        /// <param name="routeName">工艺路线名称（模糊查询）</param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ApiPageing<RouteDto>> GetRoutePage(
            [FromQuery] int index = 1,
            [FromQuery] int size = 20,
            [FromQuery] string? routeCode = null,
            [FromQuery] string? routeName = null)
        {
            var command = new GetRoutePageCommand
            {
                Index = index,
                Size = size,
                RouteCode = routeCode,
                RouteName = routeName
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取工艺路线详情（包含工序列表）
        /// </summary>
        /// <param name="id">工艺路线ID</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<RouteDetailDto>> GetRouteDetail(int id)
        {
            var command = new GetRouteDetailCommand
            {
                RouteId = id
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据工艺路线ID查询工序信息
        /// </summary>
        /// <param name="id">工艺路线ID</param>
        /// <returns>工序信息列表</returns>
        [HttpGet("{id}/steps")]
        public async Task<ApiResult<List<Dto.Route.RouteStepDto>>> GetRouteSteps(int id)
        {
            var command = new GetRouteStepsCommand
            {
                RouteId = id
            };

            return await _mediator.Send(command);
        }
    }
} 